package cn.wizzer.app.web.commons.ext.websocket.action.live.manage.session;


import cn.wizzer.app.user.modules.models.User;
import cn.wizzer.app.user.modules.models.constant.ChatConstant;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.app.web.commons.ext.websocket.main.action.BaseNettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.action.NettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.base.TextWebSocketFrameHandler;
import cn.wizzer.app.web.commons.ext.websocket.main.result.ChatResult;
import cn.wizzer.app.web.commons.utils.NettyMsgUtil;
import cn.wizzer.app.web.commons.utils.RedisUtil;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.util.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.integration.jedis.RedisService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import static cn.wizzer.app.user.modules.models.constant.ChatConstant.CHAT_USER;

/**
 * @author 22099
 */
@IocBean
@NettyAction(uri = "/manage/session/enable")
public class EnableAction extends BaseNettyAction {

    @Inject
    private RedisService redisService;
    @Inject
    @Reference
    private UserService userService;


    public static BaseNettyAction instance() {
        return BaseNettyAction.instance(EnableAction.class);
    }

    @Override
    public Result action(Channel income, JSONObject reqParams) {
        String roomId = RedisUtil.hget(User.class, ChatConstant.CHAT_USER, income.id().toString()).getRoomId();
        //用户信息
        String userStr = RedisUtil.hget(CHAT_USER, income.id().toString());
        User user = JSONUtil.parseStringToJavaBean(User.class, userStr);
        //权限
        if (     //房主
                !roomId.startsWith(user.getId())
                        //后台房管
                        && !user.isRoomManager()
                        //房主设置的房管
                        && !user.getManageRoom().contains(roomId)
        ){
            return ChatResult.sysSend(ChatResult.MessageTypeEnum.SYSTEM, ChatResult.ActionEnum.REMAND,"无成员禁言权限");
        }
        //目标
        String toChannel = String.valueOf(reqParams.get("channelId"));
        User toUser = RedisUtil.hget(User.class, CHAT_USER, toChannel);
        //判断
        if (toUser.getChannelId().equals(toUser.getId())) {
            return ChatResult.sysSend(ChatResult.MessageTypeEnum.SYSTEM, ChatResult.ActionEnum.ERROR, "游客用户禁止操作");
        }
        //禁言
        toUser.setSessionDisabled(false);
        RedisUtil.hset(CHAT_USER, toChannel, toUser);
        userService.update(Chain.make("session_disabled", false), Cnd.where("id", "=", toUser.getId()));
        NettyMsgUtil.send(TextWebSocketFrameHandler.findChannel(toChannel), ChatResult.MessageTypeEnum.ACTION, ChatResult.ActionEnum.DISABLE, null, "解除禁言");
        return ChatResult.sysSend(ChatResult.MessageTypeEnum.SYSTEM, ChatResult.ActionEnum.SUCCESS, "操作成功");
    }

    @Override
    public Result subscribe(String subscribeParam) {
        return Result.success();
    }

}
